home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*------------------------------------------------------------------------
- * Test the ability to create color icons.
- *
- * New functions used:
- * XGetIconSizes
- * XAllocIconSize
- * XAllocWMHints
- * XSetWMHints
- *
- * Events that I've figured out can be handled:
- * When created:
- * 1 ConfigureNotify
- * 2 ReparentNotify
- * While main window is up:
- * <none>
- * Turned into an icon:
- * 1 MapNotify
- * 2 VisibilityNotify
- * 3 Expose Only if icon visible
- * Cursor movement:
- * 1 EnterNotify
- * 2 MotionNotify Only 1 ever received
- * 3 LeaveNotify
- * Returned to main window:
- * 1 UnmapNotify
- * 2 LeaveNotify
- * Make icon visible:
- * 1 VisibilityNotify
- * 2 Expose
- * Button pushes within icon:
- * 1 LeaveNotify
- * 2 EnterNotify
- * Keys within icon:
- * 1 KeyPress
- * 2 KeyRelease
- *------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <unistd.h>
- #include <signal.h>
-
- #include "Initialize.h"
-
- #include <Xm/Xm.h>
- #include <Xm/Label.h>
-
- /*------------------------------------------------------------------------
- * DEBUG uses
- * 0x0001 Print Window IDs.
- * 0x0002 Print Events processed by Icon.
- * 0x0004 Print colors used.
- * 0x0008 Print information on sizes of icons allowed.
- *------------------------------------------------------------------------*/
-
- #define DEBUG 0x0000
-
- #define WINDOW_BACKGROUND "Blue"
- #define GC_BACKGROUND "Red"
- #define GC_FOREGROUND "Yellow"
- #define BLOCK_X 4
- #define BLOCK_Y 4
- #define BLOCK_HEIGHT 50
- #define BLOCK_WIDTH 50
-
- /*----
- * This structure is used to print out what event was received.
- *----*/
-
- #define TABLE_ENTRY(x) { (x), #x }
-
- struct { int value; char* string; } event_names[] = {
- TABLE_ENTRY (KeyPress),
- TABLE_ENTRY (KeyRelease),
- TABLE_ENTRY (ButtonPress),
- TABLE_ENTRY (ButtonRelease),
- TABLE_ENTRY (MotionNotify),
- TABLE_ENTRY (EnterNotify),
- TABLE_ENTRY (LeaveNotify),
- TABLE_ENTRY (FocusIn),
- TABLE_ENTRY (FocusOut),
- TABLE_ENTRY (KeymapNotify),
- TABLE_ENTRY (Expose),
- TABLE_ENTRY (GraphicsExpose),
- TABLE_ENTRY (NoExpose),
- TABLE_ENTRY (VisibilityNotify),
- TABLE_ENTRY (CreateNotify),
- TABLE_ENTRY (DestroyNotify),
- TABLE_ENTRY (UnmapNotify),
- TABLE_ENTRY (MapNotify),
- TABLE_ENTRY (MapRequest),
- TABLE_ENTRY (ReparentNotify),
- TABLE_ENTRY (ConfigureNotify),
- TABLE_ENTRY (ConfigureRequest),
- TABLE_ENTRY (GravityNotify),
- TABLE_ENTRY (ResizeRequest),
- TABLE_ENTRY (CirculateNotify),
- TABLE_ENTRY (CirculateRequest),
- TABLE_ENTRY (PropertyNotify),
- TABLE_ENTRY (SelectionClear),
- TABLE_ENTRY (SelectionRequest),
- TABLE_ENTRY (SelectionNotify),
- TABLE_ENTRY (ColormapNotify),
- TABLE_ENTRY (ClientMessage),
- TABLE_ENTRY (MappingNotify),
- };
-
- Window icon_window;
- GC graphics_context;
- Display *display;
- int block_width = BLOCK_WIDTH, block_height = BLOCK_HEIGHT;
- int block_x = BLOCK_X, block_y = BLOCK_Y;
- int icon_mapped = 0;
-
- /*------------------------------------------------------------------------
- * Set up Icon function
- * This function is called by XtAppInitializeWithIcon to perform
- * whatever is necessary to create the icon window.
- *------------------------------------------------------------------------*/
-
- Window createIconWindow (Display *dpy,
- #if XtSpecificationRelease > 4
- int *argc,
- #else
- Cardinal *argc,
- #endif
- String *argv)
- {
- int i, status;
- XIconSize *icon_sizes, *icon_size;
- int number_of_sizes;
- unsigned int border_width;
- int depth;
- Visual *visual;
- unsigned long background, border;
- XSetWindowAttributes window_attributes;
- unsigned long valuemask;
- unsigned int class;
- XColor backColor, tmpColor;
- XGCValues gc_values;
-
- /*----
- * Find out what icon sizes are allowed.
- *----*/
-
- status = XGetIconSizes (dpy, DefaultRootWindow (dpy),
- &icon_sizes, &number_of_sizes);
- if (status == 0)
- {
- fprintf (stderr, "Unable to get icon sizes from window manager.\n");
- return NULL;
- }
-
- #if DEBUG & 8
- printf ("Number of icon sizes given is %d\n", number_of_sizes);
- for (i = 0; i < number_of_sizes; i++)
- {
- icon_size = &icon_sizes[i];
- printf ("Size %d\n", i);
- printf (" Minimum width %d, height %d\n",
- icon_size->min_width, icon_size->min_height);
- printf (" Maximum width %d, height %d\n",
- icon_size->max_width, icon_size->max_height);
- printf (" Increment width %d, height %d\n",
- icon_size->width_inc, icon_size->height_inc);
- }
- #endif
-
- /*----
- * Set up the variables needed to create the icon window.
- * class As specified in the manual.
- * visual Same as root window (because it will be reparented
- * to the root window).
- * depth Same as root window.
- * border_width Whatever.
- * valuemask Attributes that I want to set. I want to get
- * some events and if I don't set the background
- * pixel value, the window comes up with garbage.
- * window_attributes.event_mask
- * This is all the possible events because I want
- * to see what events are passed on to the icon.
- *----*/
-
- class = InputOutput;
- visual = DefaultVisual (dpy, DefaultScreen (dpy));
- depth = DefaultDepth (dpy, DefaultScreen (dpy));
-
- border_width = 10; /* I made this number up */
- valuemask = CWEventMask | CWBackPixel;
- window_attributes.event_mask = (
- KeyPressMask |
- KeyReleaseMask |
- ButtonPressMask |
- ButtonReleaseMask |
- EnterWindowMask |
- LeaveWindowMask |
- PointerMotionMask |
- PointerMotionHintMask |
- Button1MotionMask |
- Button2MotionMask |
- Button3MotionMask |
- Button4MotionMask |
- Button5MotionMask |
- ButtonMotionMask |
- KeymapStateMask |
- ExposureMask |
- VisibilityChangeMask |
- StructureNotifyMask |
- ResizeRedirectMask |
- SubstructureNotifyMask |
- SubstructureRedirectMask |
- FocusChangeMask |
- PropertyChangeMask |
- ColormapChangeMask |
- OwnerGrabButtonMask);
-
- /*----
- * In order to set the background pixel value, I have to get it.
- *----*/
-
- if (!XAllocNamedColor (dpy, DefaultColormap (dpy, 0), WINDOW_BACKGROUND,
- &backColor, &tmpColor))
- fprintf (stderr, "Unable to find '%s' in database\n",
- WINDOW_BACKGROUND);
- #if DEBUG & 0x0004
- else
- {
- fprintf (stderr, "%s color is 0x%08x = %02x %02x %02x\n",
- WINDOW_BACKGROUND, backColor.pixel,
- backColor.red, backColor.green, backColor.blue);
- fprintf (stderr, "Color found is 0x%08x = %02x %02x %02x\n",
- tmpColor.pixel,
- tmpColor.red, tmpColor.green, tmpColor.blue);
- }
- #endif
-
- window_attributes.background_pixel = backColor.pixel;
-
- /*----
- * OK, now we actually create the icon window.
- *----*/
-
- icon_window = XCreateWindow (dpy, DefaultRootWindow (dpy),
- 0, 0,
- icon_sizes->max_width, icon_sizes->max_height,
- border_width, depth, class, visual,
- valuemask, &window_attributes);
-
- /*----
- * Now, for the fun of it, let's create a graphics context
- * so that other routines can draw. This could be done anywhere.
- * 1) Set up a different background color in the
- * graphics context (to find out what it does).
- * 2) Set up a foreground color.
- * 3) Define the line width.
- * 4) Define the line style.
- *----*/
-
- if (!XAllocNamedColor (dpy, DefaultColormap (dpy, 0), GC_BACKGROUND,
- &backColor, &tmpColor))
- fprintf (stderr, "Unable to find '%s' in database\n",
- GC_BACKGROUND);
- #if DEBUG & 0x0004
- else
- {
- fprintf (stderr, "%s color is 0x%08x = %02x %02x %02x\n",
- GC_BACKGROUND, backColor.pixel,
- backColor.red, backColor.green, backColor.blue);
- fprintf (stderr, "Color found is 0x%08x = %02x %02x %02x\n",
- tmpColor.pixel,
- tmpColor.red, tmpColor.green, tmpColor.blue);
- }
- #endif
-
- gc_values.background = backColor.pixel;
-
- if (!XAllocNamedColor (dpy, DefaultColormap (dpy, 0), GC_FOREGROUND,
- &backColor, &tmpColor))
- fprintf (stderr, "Unable to find '%s' in database\n",
- GC_FOREGROUND);
- #if DEBUG & 0x0004
- else
- {
- fprintf (stderr, "%s color is 0x%08x = %02x %02x %02x\n",
- GC_FOREGROUND, backColor.pixel,
- backColor.red, backColor.green, backColor.blue);
- fprintf (stderr, "Color found is 0x%08x = %02x %02x %02x\n",
- tmpColor.pixel,
- tmpColor.red, tmpColor.green, tmpColor.blue);
- }
- #endif
-
- gc_values.foreground = backColor.pixel;
- gc_values.line_width = 1;
- gc_values.line_style = LineSolid;
- graphics_context = XCreateGC (dpy, icon_window,
- (GCForeground | GCBackground | GCLineWidth |
- GCLineStyle),
- &gc_values);
-
- return icon_window;
- }
-
- /*------------------------------------------------------------------------
- * find_table_entry
- * Just a little function for determining the index into the
- * event_names table that a particular event is described in.
- * This allows us to print the name of the event when it is
- * processed.
- *------------------------------------------------------------------------*/
-
- int find_table_entry (int val)
- {
- int i;
-
- for (i = 0; i < XtNumber (event_names); i++)
- {
- if (event_names[i].value == val)
- return i;
- }
-
- return -1;
- }
-
- /*------------------------------------------------------------------------
- * draw_icon
- * Just a simple routine which is called whenever we need to
- * redraw the contents of the icon.
- *------------------------------------------------------------------------*/
-
- void draw_icon (int motion, XEvent event)
- {
- static int direction = 1;
-
- if (motion == 2)
- {
- if (direction)
- {
- block_x++;
- block_width -= 2;
- if (block_width <= 1)
- direction--;
- }
- else
- {
- block_x--;
- block_width += 2;
- if (block_width == BLOCK_WIDTH)
- direction++;
- }
- }
-
- XClearWindow (display, icon_window);
- XFillRectangle (display, icon_window, graphics_context, block_x, block_y,
- block_width, block_height);
-
- XSync (display, FALSE);
- }
-
- /*------------------------------------------------------------------------
- * update Update the icon when alarm occurs.
- *------------------------------------------------------------------------*/
-
- void update ()
- {
- XEvent event;
-
- if (icon_mapped)
- {
- draw_icon (2, event);
- alarm (1);
- }
- }
-
- /*------------------------------------------------------------------------
- * MAIN
- *------------------------------------------------------------------------*/
-
- main (int argc, char *argv[])
- {
- XtAppContext app_context;
- Widget toplevel, hello;
- XmString xmstr;
- Arg wargs[10];
- Window window;
- XEvent event;
-
- toplevel = XtAppInitializeWithIcon (&app_context, "TextIcon", NULL, 0,
- #if XtSpecificationRelease > 4
- &argc,
- #else
- (Cardinal *)&argc,
- #endif
- argv, NULL, NULL, 0,
- (makeIcon)createIconWindow);
-
- display = XtDisplay (toplevel);
-
- hello = XmCreateLabel (toplevel, "hello", NULL, 0);
- XtManageChild (hello);
-
- XtRealizeWidget (toplevel);
- window = XtWindow (toplevel);
-
- #if DEBUG & 0x0001
- fprintf (stderr, "toplevel window = 0x%08x\n", window);
- fprintf (stderr, "icon window = 0x%08x\n", icon_window);
- #endif
-
- /*----
- * Do the realizing and other standard X stuff.
- *----*/
-
- xmstr = XmStringCreateSimple ("hello");
- XtVaSetValues (hello, XmNlabelString, xmstr, NULL);
- XmStringFree (xmstr);
-
- /*----
- * Set up signal handler.
- *----*/
-
- sigset (SIGALRM, update);
-
- /*----
- * This is the MAIN LOOP.
- *
- * I have it expanded out so that I can process the events that are
- * given to the icon window. This is just for fun so I can find
- * out what events are processed.
- *----*/
-
- while (1)
- {
- XtAppNextEvent (app_context, &event);
- if (event.xany.window == icon_window)
- {
- #if DEBUG & 0x0002
- fprintf (stderr, "Icon received event #%d - %s\n", event.type,
- event_names[find_table_entry (event.type)].string);
- #endif
-
- if (event.type == MapNotify)
- {
- icon_mapped = 1;
- alarm (1);
- }
- else if (event.type == UnmapNotify)
- {
- icon_mapped = 0;
- alarm (0);
- }
- else if (event.type == Expose)
- {
- draw_icon (0, event);
- }
- else if (event.type == MotionNotify)
- {
- draw_icon (0, event);
- }
- else if (event.type == KeyRelease)
- {
- draw_icon (1, event);
- }
- }
- else
- {
- XtDispatchEvent (&event);
- }
- }
- }
-